﻿CREATE PROCEDURE [ServiceEngine].[DeleteEntityGroup]
 @ID uniqueidentifier,
 @Name nvarchar(400) OUTPUT
AS
BEGIN
 DECLARE @ErrorMessage NVARCHAR(4000);
 DECLARE @ErrorSeverity INT;
 DECLARE @ErrorState INT;
 SET NOCOUNT ON;

 SELECT 
   @Name = N.[EntityName]
 FROM [ServiceEngine].Names N 
 WHERE N.[EntityID]  = @ID and N.[EntityTypeID] = 11
 
 BEGIN TRY
  BEGIN TRANSACTION

  DELETE ServiceEngine.EntityGroups 
  WHERE EntityGroupID = @ID

  DELETE ServiceEngine.Names
  WHERE EntityID = @ID and EntityTypeID = 11

  COMMIT TRANSACTION
 END TRY
 BEGIN CATCH
  SELECT 
   @ErrorMessage = ERROR_MESSAGE(),
   @ErrorSeverity = ERROR_SEVERITY(),
   @ErrorState = ERROR_STATE();

  IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;

  RAISERROR (@ErrorMessage, 
       @ErrorSeverity,
       1);
 END CATCH

END
